import sensor, image
import time

import json
import ustruct
from pyb import UART

uart = UART(3,115200)   #定义串口3变量
uart.init(115200, bits=8, parity=None, stop=1) # RX:P5 TX:P4

def sending_data(task):
    global uart;
    data = ustruct.pack("<bbbbbbbb",   #定义要打包的格式化字符串 f:float i:int
                        0x2C,            #帧头1
                        0x12,            #帧头2
                        ord(task[0]),
                        ord(task[1]),
                        ord(task[2]),
                        ord(task[4]),
                        ord(task[5]),
                        ord(task[6]))
    uart.write(data);               #必须要传入一个字节数组

task = 0
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA) # can be QVGA on M7...
sensor.set_vflip(True)
sensor.set_hmirror(True)# 水平方向翻转
sensor.set_auto_exposure(True)
sensor.skip_frames(30)

while(True):
    img = sensor.snapshot().lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.

    #top_tilt = 10 # if the difference between top/bottom_tilt become to large this method will stop working
    #bottom_tilt = 0

    #points = [(tilt, 0), (img.width()-tilt, 0), (img.width()-1-bottom_tilt, img.height()-1), (bottom_tilt, img.height()-1)]

    img = img.rotation_corr(x_rotation = 10)
    for code in img.find_qrcodes():
        val = code.payload()
        sending_data(val)
